package com.techyuan.algorithm.structure.tree;

/**
 * Created by tianjuntao on 2017/1/9.
 */
public class TreeUtils {
    //中序遍历
    public static <T extends TreeNode> void inorderTreeWalk(TreeNode<T> node) {
        if (!node.isNil()) {
            inorderTreeWalk(node.getLeft());
            System.out.print(node + ",");
            inorderTreeWalk(node.getRight());
        }
    }

    //先序遍历'
    public static <T extends TreeNode> void preorderTreeWalk(TreeNode<T> node) {
        if (!node.isNil()) {
            System.out.print(node + ",");
            preorderTreeWalk(node.getLeft());
            preorderTreeWalk(node.getRight());
        }
    }

    //后序遍历
    public static <T extends TreeNode> void postorderTreeWalk(TreeNode<T> node) {
        if (!node.isNil()) {
            postorderTreeWalk(node.getLeft());
            postorderTreeWalk(node.getRight());
            System.out.print(node + ",");
        }
    }

    //后序遍历
    public static <T extends TreeNode> int getTreeSize(TreeNode<T> node, TreeNode<T> nil) {
        int size = 0;
        if (!node.isNil()) {
            size += getTreeSize(node.getLeft(), nil);
            size += getTreeSize(node.getRight(), nil);
            size += 1;
        }
        return size;
    }
}
